package com.shangcheng.controller;

import com.shangcheng.common.Result;
import com.shangcheng.entity.TCustomer;
import com.shangcheng.entity.TUsers;
import com.shangcheng.service.TUsersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RequestMapping({"/public"})
@RestController
public class UsersController {
    @Autowired
    private TUsersServiceImpl usersService;
    @PostMapping("/ulogin")
    public Result<Map<String, Object>> ulogin(@RequestBody Map<String, String> uloginMap) {
        Result<Map<String, Object>> re = Result.ok((Map<String, Object>) null);
        String userName = (String)uloginMap.get("username");
        String passWord = (String)uloginMap.get("password");
        TUsers retUser = this.usersService.queryByUsername(userName);
        if (retUser == null) {
            re.setCode(100);
            re.setMessage("用户名错误！");
        } else if (!retUser.getPassword().equals(passWord)) {
            re.setCode(101);
            re.setMessage("密码错误！");
        } else {
            Map<String, Object> map = new HashMap();
            map.put("name", retUser.getName());
            map.put("id", retUser.getId());
            map.put("username", retUser.getUserName());
            map.put("token", this.usersService.createTocken(retUser));
            re.setData(map);
            re.setMessage("登陆成功！");
        }

        return re;
    }

}
